package com.wing.order.controller.app;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.enterprise.feign.EnterpriseClient;
import com.wing.enterprise.product.model.response.ProductDetailVO;
import com.wing.enterprise.product.model.response.ProductUnitVO;
import com.wing.enterprise.product.model.response.ProductVO;
import com.wing.order.model.entity.InventoryOutPlan;
import com.wing.order.model.entity.InventoryOutPlanDetail;
import com.wing.order.model.request.InventoryOutPlanForm;
import com.wing.order.model.response.InventoryOutPlanDetailVO;
import com.wing.order.model.response.InventoryOutPlanVO;
import com.wing.order.service.InventoryOutPlanDetailService;
import com.wing.order.service.InventoryOutPlanService;
import com.wing.warehouse.model.entity.Warehouse;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-出库单计划管理", tags = "#/order/inventoryOutPlan")
@RestController("appInventoryOutPlanController")
@RequestMapping("order/app/inventoryOutPlan")
@RequiredArgsConstructor
public class InventoryOutPlanController {

    private final InventoryOutPlanService inventoryOutPlanService;
    private final InventoryOutPlanDetailService inventoryOutPlanDetailService;
    private final EnterpriseClient enterpriseClient;

    @ApiOperation(value = "查询所有出库单计划", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<InventoryOutPlan> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,InventoryOutPlan inventoryOutPlan
    ){

        //搜索
        IPage<InventoryOutPlan> list = inventoryOutPlanService.getInventoryOutPlanPageList(inventoryOutPlan,page,limit);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "修改出库单计划", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(InventoryOutPlanForm.Update.class) InventoryOutPlanForm inventoryOutPlanForm) {
        InventoryOutPlan inventoryOutPlan = inventoryOutPlanService.getById(inventoryOutPlanForm.getId());
        if(inventoryOutPlan == null){
        return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(InventoryOutPlan.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(inventoryOutPlanForm, inventoryOutPlan, copyOptions);
        if (inventoryOutPlanService.updateById(inventoryOutPlan)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }

    @ApiOperation(value = "出库单计划x详情", notes = "查询")
    @GetMapping("/getInventoryOutPlanDetailById")
    public JsonResult getInventoryOutPlanDetailById(@RequestParam(value = "id") Long id){
        InventoryOutPlanVO info = inventoryOutPlanService.getInventoryOutPlanDetailById(id);
        return JsonResult.success(info);
    }

    @ApiOperation(value = "审核出库单计划", notes = "修改")
    @PutMapping("/updateStatus")
    public JsonResult updateStatus(@RequestParam(value = "id") Long id,@RequestParam(value = "status") String status,@RequestParam(value = "remark") String remark) {
        InventoryOutPlan inventoryOutPlan = inventoryOutPlanService.getById(id);
        if(inventoryOutPlan == null){
            return JsonResult.fail("修改失败！");
        }
        if(status.equals(InventoryOutPlan.Status.confirmed.getValue().toString())){
            inventoryOutPlan.setStatus(InventoryOutPlan.Status.confirmed);
        }else if(status.equals(InventoryOutPlan.Status.confirm.getValue().toString())){
            inventoryOutPlan.setStatus(InventoryOutPlan.Status.confirm);
        }
        if(StringUtils.isNotBlank(remark)){
            inventoryOutPlan.setRemark(remark);
        }
        if (inventoryOutPlanService.updateById(inventoryOutPlan)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }

}
